package org.fuxia.im.core.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.fuxia.im.core.entries.ECPacket;
import org.fuxia.im.core.entries.ECResponse;
import org.fuxia.im.core.server.ECSessionMananger;
import org.fuxia.im.core.socket.impl.ECNioServer;
import org.fuxia.im.db.dao.ChatGroupDaoImpl;
import org.fuxia.im.db.dao.UserChatGroupDaoImpl;

public class GroupOptionService extends AbstractService {

	@Resource
	private UserChatGroupDaoImpl ucgDao ;
	
	@Resource
	private ChatGroupDaoImpl chatGroupDao ;
	
	public void service(ECPacket packet) {
		String message = "" ;
		String option = packet.getParameter("option") ;
		String groupId = packet.getParameter("groupId") ;
		if(option.equals("dismissGroup")) {
			try{
				List<Object[]> members = chatGroupDao.findChatGroupUsers(groupId) ;
				if(members!=null) {
					String sql = "delete from UserChatGroup where userCode='%s' and chatGroupId='%s'" ;
					for(Object[] o : members) {
						ucgDao.bulkUpdate(String.format(sql, o[0].toString(),groupId)) ;
						writeResponseMessage(o[0].toString(),groupId);
					}
				}
				chatGroupDao.bulkUpdate("delete from ChatGroup cg where  cg.id='" + groupId + "'") ;
				message = "群组解散成功";
			}catch (Exception e) {
				message = "群组解散失败";
			}
			
		}else  if(option.equals("exitGroup")){
			String sql = String.format("delete from UserChatGroup ucg where ucg.userCode='%s' and ucg.chatGroupId='%s'",
					packet.getFrom(),groupId);
			try{
				ucgDao.bulkUpdate(sql);
				message = "退出群组成功";
				ECResponse resp = new ECResponse();
				resp.setResponseText("用户[" + packet.getFrom()+"]退出了群组") ;
				resp.setResponseType("GROUP_OPTION") ;
				resp.put("user", packet.getFrom());
				resp.put("groupId", groupId);
				resp.setStatusCode(300) ;
				if(ECSessionMananger.getInstance().hasLogin(packet.getParameter("owner"))) {
					try{
						ECNioServer.writeResponse(resp, ECSessionMananger.getInstance().
													getUserSession(packet.getParameter("owner")).getChannel());
					}catch (Exception e) {
					}
				}
			}catch (Exception e) {
				message = "退出群组失败";
			}
		}else if(option.equals("removeUser")) {
			String sql = String.format("delete from UserChatGroup ucg where ucg.userCode='%s' and ucg.chatGroupId='%s'",
					packet.getParameter("userCode"),groupId);
			try{
				ucgDao.bulkUpdate(sql);
				message = "移除成员成功";
				ECResponse resp = new ECResponse();
				resp.setResponseText("你被移除了群组【"+groupId+"】") ;
				resp.setResponseType("GROUP_OPTION") ;
				resp.setStatusCode(500) ;
				resp.put("groupId", groupId);
				if(ECSessionMananger.getInstance().hasLogin(packet.getParameter("userCode"))) {
					try{
						ECNioServer.writeResponse(resp, ECSessionMananger.getInstance().
													getUserSession(packet.getParameter("userCode")).getChannel());
					}catch (Exception e) {
					}
				}
			}catch (Exception e) {
				message = "移除成员失败";
			}
		}
		
		ECResponse resp = new ECResponse() ;
		resp.setStatusCode(200);
		resp.setResponseText(message);
		writeReponse(packet, resp);
	}
	
	private void writeResponseMessage(String userCode,String groupId) {
		ECResponse resp = new ECResponse() ;
		resp.setStatusCode(600);
		resp.setResponseType("GROUP_OPTION");
		resp.setResponseText("聊天群[" + groupId+"]已经解散");
		resp.put("groupId", groupId) ;
		if(ECSessionMananger.getInstance().hasLogin(userCode)) {
			try{
				ECNioServer.writeResponse(resp, 
							ECSessionMananger.getInstance().getUserSession(userCode).getChannel()) ;
			}catch (Exception e) {
			}
		}
	}
}
